/******************************************************************************/
/*  Class MB_MASTER                                                           */
/*  PO: S. Maslyakov, rusoil.9@gmail.com                                      */
/*                                                                            */
/*  Revision:     1.0                                                         */
/*  Date:         2011/01/20 11:53:33                                         */
/******************************************************************************/


#ifndef __MB_MASTER_H
#define __MB_MASTER_H


#include "Uart\uart2.h"
#include "mb_master_def.h"


//==============================================================================
//  Class MB_MASTER
//==============================================================================
class MB_MASTER : public UART2
{
public:
  static void Init_MbMaster();
  static sint32_t OpenMbMaster(const uint32_t _baudrate, const uint8_t _key,
                               const uint8_t _slave, const uint8_t _func);
  static void CloseMbMaster();
  static void InterruptHandlerUart2();
  static void Init_mb_task();
  static void * Get_mb_event();
  static void * Get_mb_cmd_dque();

private:
  static void Init_rtos_part();
  static void Modbus_change_order_bytes(uint8_t * const _pBuff,
                                        const uint32_t _dataLen);

  static uint32_t Compute_response_length(const uint8_t * const _pQuery);

  static uint32_t Build_query_basis_rtu(const uint8_t _slave,
                                        const uint8_t _function,
                                        const uint16_t _startAddr,
                                        const uint16_t _nb,
                                        uint8_t * const _pQuery);

  static sint32_t Check_crc16(const uint8_t * const _pMsg,
                              const uint32_t _msgLength);

  static uint32_t Modbus_add_crc(uint8_t * const _pQuery,
                                 const uint32_t _queryLength);

  static void Read_io_status(const uint8_t _slave, const uint8_t _function,
                             const uint16_t _startAddr, const uint16_t _nb);

  static sint32_t Read_registers(const uint8_t _slave, const uint8_t _function,
                                 const uint16_t _startAddr, const uint16_t _nb);

  static void Set_single(const uint8_t _slave, const uint8_t _function,
                         const uint16_t _addr, const uint16_t _value);

  static sint32_t Read_coil_status(const uint8_t _slave,
                                   const uint16_t _startAddr,
                                   const uint16_t _nb);

  static sint32_t Read_input_status(const uint8_t _slave,
                                    const uint16_t _startAddr,
                                    const uint16_t _nb);

  static sint32_t Read_holding_registers(const uint8_t _slave,
                                         const uint16_t _startAddr,
                                         const uint16_t _nb);

  static sint32_t Read_input_registers(const uint8_t _slave,
                                       const uint16_t _startAddr,
                                       const uint16_t _nb);

  static void Force_single_coil(const uint8_t _slave,
                                const uint16_t _coilAddr,
                                uint32_t _state);

  static void Preset_single_register(const uint8_t _slave,
                                     const uint16_t _regAddr,
                                     const uint16_t _value);

  static sint32_t Force_multiple_coils(const uint8_t _slave,
                                       const uint16_t _startAddr,
                                       const uint16_t _nb,
                                       const uint8_t * const _pData);

  static sint32_t Preset_multiple_registers(const uint8_t _slave,
                                            const uint16_t _startAddr,
                                            const uint16_t _nb,
                                            const uint8_t * const _pData);

  static sint32_t User_func(const uint8_t _slave,
                            const uint8_t _function,
                            const uint16_t _dataLen,
                            const uint8_t * const _pData);

  static sint32_t Modbus_make_query(const __MB_QUERY_BUILD * const _pQueryData);
  static sint32_t Modbus_receive(const uint8_t _byte);
  static sint32_t Modbus_process_answ();
  static void Task_mb_poll(void * par);
  static void Task_mb_rx(void * par);

  static __MB_ANSW_READY_DATA * m_pAnsw;
  static __MB_QUERY_TOOLS m_query;
};
//==============================================================================
//==============================================================================


#endif